﻿namespace Fiddler
{
    using Microsoft.Win32;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Threading;

    internal class MRU
    {
        private int _iMaxFileCount;
        private string _sMutexName;
        private string _sRegPath;
        private List<string> slItems;

        public MRU(string sRegPath, int iMaxFileCount)
        {
            if (iMaxFileCount < 0)
            {
                throw new ArgumentException("Max file count must be >0");
            }
            this._iMaxFileCount = iMaxFileCount;
            this._sRegPath = sRegPath;
            this._sMutexName = string.Format(@"Global\MRU_{0}_{1}", Environment.UserName, sRegPath.Replace('\\', '_'));
        }

        private void _FilterNonExistentFiles()
        {
            List<string> list = new List<string>();
            if (this.slItems != null)
            {
                foreach (string str in this.slItems)
                {
                    try
                    {
                        if (File.Exists(str))
                        {
                            list.Add(str);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            this.slItems = list;
        }

        public string[] GetList()
        {
            if (this.slItems == null)
            {
                return new string[0];
            }
            return this.slItems.ToArray();
        }

        public string GetListAsString()
        {
            if (this.slItems == null)
            {
                return string.Empty;
            }
            return string.Join("*", this.slItems.ToArray());
        }

        public void PruneObsolete()
        {
            this.slItems = new List<string>();
            Mutex mutex = new Mutex(false, this._sMutexName);
            if (mutex.WaitOne(0x3e8, false))
            {
                try
                {
                    RegistryKey key = Registry.CurrentUser.OpenSubKey(this._sRegPath, RegistryKeyPermissionCheck.ReadWriteSubTree);
                    if (key != null)
                    {
                        string listAsString = (string)key.GetValue("MRU");
                        if (!string.IsNullOrEmpty(listAsString))
                        {
                            this.slItems.AddRange(listAsString.Split(new char[] { '*' }, StringSplitOptions.RemoveEmptyEntries));
                            this._FilterNonExistentFiles();
                            listAsString = this.GetListAsString();
                            key.SetValue("MRU", listAsString);
                        }
                    }
                    key.Close();
                }
                finally
                {
                    mutex.ReleaseMutex();
                }
            }
        }

        public void PurgeAll()
        {
            this.slItems = null;
            Mutex mutex = new Mutex(false, this._sMutexName);
            if (mutex.WaitOne(0x3e8, false))
            {
                try
                {
                    RegistryKey key = Registry.CurrentUser.OpenSubKey(this._sRegPath, RegistryKeyPermissionCheck.ReadWriteSubTree);
                    if (key != null)
                    {
                        key.DeleteValue("MRU", false);
                        key.Close();
                    }
                }
                finally
                {
                    mutex.ReleaseMutex();
                }
            }
        }

        public void PushFile(string sFilename)
        {
            Predicate<string> match = null;
            if ((((this._iMaxFileCount != 0) && !string.IsNullOrEmpty(sFilename)) && !sFilename.EndsWith(".dat", StringComparison.OrdinalIgnoreCase)) && (sFilename.LastIndexOf(@"\_") != sFilename.LastIndexOf('\\')))
            {
                Mutex mutex = new Mutex(false, this._sMutexName);
                if (mutex.WaitOne(0x1388, false))
                {
                    try
                    {
                        if (this.RefreshList())
                        {
                            if (match == null)
                            {
                                match = delegate(string s)
                                {
                                    return string.Equals(sFilename, s, StringComparison.OrdinalIgnoreCase);
                                };
                            }
                            this.slItems.RemoveAll(match);
                            this.slItems.Insert(0, sFilename);
                            if (this.slItems.Count > this._iMaxFileCount)
                            {
                                this.slItems.RemoveRange(this._iMaxFileCount, this.slItems.Count - this._iMaxFileCount);
                            }
                            string listAsString = this.GetListAsString();
                            RegistryKey key = Registry.CurrentUser.CreateSubKey(this._sRegPath);
                            if (key != null)
                            {
                                key.SetValue("MRU", listAsString);
                                key.Close();
                            }
                        }
                    }
                    finally
                    {
                        mutex.ReleaseMutex();
                    }
                }
            }
        }

        public bool RefreshList()
        {
            if (this._iMaxFileCount == 0)
            {
                return true;
            }
            bool flag = false;
            this.slItems = new List<string>();
            string str = null;
            try
            {
                flag = true;
                RegistryKey key = Registry.CurrentUser.OpenSubKey(this._sRegPath, RegistryKeyPermissionCheck.ReadSubTree);
                if (key != null)
                {
                    str = (string)key.GetValue("MRU");
                    key.Close();
                }
            }
            catch
            {
                flag = false;
                str = null;
            }
            if (!string.IsNullOrEmpty(str))
            {
                this.slItems.AddRange(str.Split(new char[] { '*' }, this._iMaxFileCount, StringSplitOptions.RemoveEmptyEntries));
            }
            return flag;
        }
    }
}

